﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;

namespace HZ.DrugTrade.Web.Hospitals.Procurement
{
    /// <summary>
    /// HospitalEvent 的摘要说明
    /// </summary>
    public class HospitalEvent : IHttpHandler, IRequiresSessionState  
    {

        public void ProcessRequest(HttpContext context)
        {
            #region MyRegion


            //JavaScriptSerializer jsS = new JavaScriptSerializer();
            //string result = "";
            //DataSet ds = new DataSet();
            //List<object> lists = new List<object>();
            //string i = context.Request["i"];
            //string ProcureCatalogID = context.Request["ProcureCatalogID"];
            //string sql1 = string.Format("select*from TB_HistoryPrice where ProcureCatalogID='{0}'", ProcureCatalogID);
            //ds = HZ.Data.DbHelperFactory.Instance.ExecuteDataSet(sql1);
            //foreach (DataRow dr in ds.Tables[0].Rows)
            //{
            //    // var obj = new { name = dr["HospitalName"], value = dr["PurchaseAmount"] };
            //    var obj = new { name = dr["HospitalName"], group = dr["PurchaseAmount"], value_d = dr["DistributeAmount"], value = dr["WarehouseAmount"] };
            //    lists.Add(obj);

            //}
            //jsS = new JavaScriptSerializer();
            //result = jsS.Serialize(lists);
            //context.Response.Write(result);
            //this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "Drawline(" + result + ", 'ctl00_ContentPlaceHolder1_echart');", true);
            #endregion
            #region 九断线
            try
            {
                string i = context.Request["i"];
                string ProcureCatalogID = context.Request["ProcureCatalogID"];
                if (!string.IsNullOrEmpty(i))
                {
                    string sql = string.Format("SELECT DISTINCT weici FROM ( SELECT PROCURECATALOGID,Price ,DENSE_RANK() OVER ( PARTITION BY ProcureCatalogID ORDER BY ProcureCatalogID, price ) weici FROM ( SELECT ProcureCatalogID ,Price FROM TB_Location  WHERE  ProcureCatalogID = '{0}' UNION ALL  SELECT    '{0}',{1} from dual) t ) tb  WHERE  Price ='{1}'", ProcureCatalogID, i);
                    string sql1 = string.Format("select*from TB_HistoryPrice where ProcureCatalogID='{0}'", ProcureCatalogID);
                    System.Data.DataSet dts = new System.Data.DataSet();
                    dts = HZ.Data.DbHelperFactory.Instance.ExecuteDataSet(sql);
                    System.Data.DataSet ds = new System.Data.DataSet();
                    ds = HZ.Data.DbHelperFactory.Instance.ExecuteDataSet(sql1);

                    string minprice = "";
                    string maxprice = "";
                    string avgprice = "";
                    string middleprice = "";
                    string Weighting = "";
                    string REFPRICE = "";
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        minprice = ds.Tables[0].Rows[0]["minprice"] == null ? "" : ds.Tables[0].Rows[0]["minprice"].ToString();
                        maxprice = ds.Tables[0].Rows[0]["maxprice"] == null ? "" : ds.Tables[0].Rows[0]["maxprice"].ToString();
                        avgprice = ds.Tables[0].Rows[0]["avgprice"] == null ? "" : ds.Tables[0].Rows[0]["avgprice"].ToString();
                        middleprice = ds.Tables[0].Rows[0]["middleprice"] == null ? "" : ds.Tables[0].Rows[0]["middleprice"].ToString();
                        Weighting = ds.Tables[0].Rows[0]["Weighting"] == null ? "" : ds.Tables[0].Rows[0]["Weighting"].ToString();
                        REFPRICE = ds.Tables[0].Rows[0]["REFPRICE"] == null ? "" : ds.Tables[0].Rows[0]["REFPRICE"].ToString();
                    }
                    string weici = "";
                    if (dts.Tables[0].Rows.Count > 0)
                    {
                        weici = dts.Tables[0].Rows[0]["weici"] == null ? "" : dts.Tables[0].Rows[0]["weici"].ToString();
                    }
                    string notice = "";
                    if (weici != "")
                    {
                        notice = notice + "当前位次【" + weici.ToString() + "】；";
                    }
                    if (minprice != "")
                    {
                        notice = notice + "历史采购最低价【" + minprice + "】；";
                    }
                    if (maxprice != "")
                    {
                        notice = notice + "历史采购最高价【" + maxprice + "】；";
                    }
                    if (avgprice != "")
                    {
                        notice = notice + "历史采购平均价【" + avgprice + "】；";
                    }
                    if (middleprice != "")
                    {
                        notice = notice + "历史采购中位价【" + middleprice + "】；";
                    }
                    if (Weighting != "")
                    {
                        notice = notice + "历史采购加权平均价【" + Weighting + "】；";
                    }
                    if (REFPRICE != "")
                    {
                        notice = notice + "参考价【" + REFPRICE + "】；";
                    }
                    //计算显示图片
                    int pic = 0;
                    //if (maxprice == "" && Convert.ToDecimal(i) > Convert.ToDecimal(minprice))
                    //{
                    //    pic = 7;
                    //}
                    if (Convert.ToDecimal(i) <= Convert.ToDecimal(string.IsNullOrEmpty(minprice) == false ? minprice : "0"))
                    {
                        pic = 3;
                    }
                    else if (Convert.ToDecimal(i) >= Convert.ToDecimal(string.IsNullOrEmpty(maxprice) == false ? maxprice : "0"))
                    {
                        pic = 7;
                    }
                    else
                    {

                        if (maxprice != "")
                        {
                            decimal ii = (Convert.ToDecimal(maxprice) - Convert.ToDecimal(minprice)) / Convert.ToDecimal(9.00);
                            if (Convert.ToDecimal(i) == Convert.ToDecimal(avgprice))
                            {
                                pic = 5;
                            }
                            else if (Convert.ToDecimal(i) > Convert.ToDecimal(avgprice))
                            {
                                if ((Convert.ToDecimal(i) - Convert.ToDecimal(avgprice)) / ii < 1)
                                {
                                    pic = 5;
                                }
                                else if ((Convert.ToDecimal(i) - Convert.ToDecimal(avgprice)) / ii >= 1 && (Convert.ToDecimal(i) - Convert.ToDecimal(avgprice)) / ii < 2)
                                {
                                    pic = 4;
                                }
                                else if ((Convert.ToDecimal(i) - Convert.ToDecimal(avgprice)) / ii >= 2 && (Convert.ToDecimal(i) - Convert.ToDecimal(avgprice)) / ii < 3)
                                {
                                    pic = 9;
                                }
                                else if ((Convert.ToDecimal(i) - Convert.ToDecimal(avgprice)) / ii >= 3 && (Convert.ToDecimal(i) - Convert.ToDecimal(avgprice)) / ii < 4)
                                {
                                    pic = 8;
                                }
                                else if ((Convert.ToDecimal(i) - Convert.ToDecimal(avgprice)) / ii >= 4)
                                {
                                    pic = 7;
                                }
                            }
                            else if (Convert.ToDecimal(i) < Convert.ToDecimal(avgprice))
                            {
                                if ((Convert.ToDecimal(avgprice) - Convert.ToDecimal(i)) / ii < 1)
                                {
                                    pic = 5;
                                }
                                else if ((Convert.ToDecimal(avgprice) - Convert.ToDecimal(i)) / ii >= 1 && (Convert.ToDecimal(avgprice) - Convert.ToDecimal(i)) / ii < 2)
                                {
                                    decimal a = (Convert.ToDecimal(avgprice) - Convert.ToDecimal(i)) / ii;
                                    pic = 6;
                                }
                                else if ((Convert.ToDecimal(avgprice) - Convert.ToDecimal(i)) / ii >= 2 && (Convert.ToDecimal(avgprice) - Convert.ToDecimal(i)) / ii < 3)
                                {
                                    pic = 1;
                                }
                                else if ((Convert.ToDecimal(avgprice) - Convert.ToDecimal(i)) / ii >= 3 && (Convert.ToDecimal(avgprice) - Convert.ToDecimal(i)) / ii < 4)
                                {
                                    pic = 2;
                                }
                                else if ((Convert.ToDecimal(avgprice) - Convert.ToDecimal(i)) / ii >= 4)
                                {
                                    pic = 3;
                                }
                            }
                        }
                        else if (minprice == "" || i == minprice)
                        {
                            pic = 3;
                        }
                        else
                        {
                            pic = 7;
                        }
                    }
                    context.Response.Write(notice + "$" + pic.ToString());
                }
                else
                {
                    context.Response.Write("");
                }



            }
            catch (Exception)
            {

                throw;
            }
            #endregion

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}